﻿using System;
using System.IO;
using System.Windows.Forms;

namespace Log
{
    // 推送测试
    public sealed class C
    {
        public const string Platform = "CLIENT";

        public static void Debug(string msg)
        {
#if DEBUG

            LogtoTextDebug("debug", "CLIENT", msg);
#endif

        }

        public static void Debug(string format, params object[] args)
        {
#if DEBUG
            try
            {
                LogtoTextDebug("debug", "CLIENT", string.Format(format, args));
            }
            catch
            {
            }
#endif
        }


        public static void OpenLog()
        {
            string str = Path.Combine(Application.StartupPath, "logs");
            string path = Path.Combine(str, DateTime.Now.ToString("yyyyMMdd") + ".log");
            if (!Directory.Exists(str))
            {
                Directory.CreateDirectory(str);
            }
            if (!File.Exists(path))
            {
                File.Create(path);
            }
            System.Diagnostics.Process.Start($"\"{path}\"");
        }
        public static event EventHandler<LogEventArgs> OnLogEvent;
        public static void LogtoTextDebug(string model, string method, string content,bool writeTofile = true)
        {
#if DEBUG
            try
            {
                string str = Path.Combine(Application.StartupPath, "logs");
                string path = Path.Combine(str, DateTime.Now.ToString("yyyyMMdd") + ".log");
                if (!Directory.Exists(str))
                {
                    Directory.CreateDirectory(str);
                }
                var str3 = $"{DateTime.Now.ToString("hh:mm:ss")}\t{model}\t{method}\t{content}";
                OnLogEvent?.Invoke(null, new LogEventArgs() { StrLog = str3 });
                if (writeTofile)
                {
                    using (FileStream stream = File.Open(path, FileMode.Append, FileAccess.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {

                            writer.WriteLine(str3);
                            writer.Close();
                            writer.Dispose();
                            stream.Close();
                            stream.Dispose();
                        }
                    }
                }
               
            }
            catch (Exception)
            {
                
                //throw;
            }
            
#endif
        }
        public static void LogtoTextRelease(string model, string method, string content)
        {
            string str = Path.Combine(Application.StartupPath, "logs");
            string path = Path.Combine(str, DateTime.Now.ToString("yyyyMMdd") + ".log");
            if (!Directory.Exists(str))
            {
                Directory.CreateDirectory(str);
            }
            using (FileStream stream = File.Open(path, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    var str3 = $"{DateTime.Now.ToString("hh:mm:ss")}\t{model}\t{method}\t{content}";
                    writer.WriteLine(str3);
                    writer.Close();
                    writer.Dispose();
                    stream.Close();
                    stream.Dispose();
                }
            }
        }
    }

    public class LogEventArgs : EventArgs
    {
        public string StrLog;
    }
}

